import { emptyState } from './state'

const mutationObj = {
  resetState (state) {
    state.site = { ...emptyState.site }
    state.projectList = { ...emptyState.projectList }
    state.company = { ...emptyState.company }
    state.project = { ...emptyState.project }
    state.user = { ...emptyState.user }
  },
  setSetting (state, obj) {
    state.setting = {
      showSidebar: obj.showSidebar || '1',
    }
  },
  updateSetting (state, obj) {
    state.setting = { ...state.setting, ...obj }
  },
  setSite (state, obj) {
    state.site = {
      title: obj.title || '',
      desc: obj.desc || '',
      homePage: obj.homePage || '',
    }
  },
  updateSite (state, obj) {
    state.site = { ...state.site, ...obj }
  },
  setProject (state, obj) {
    state.project = {
      projectId: obj.projectId || '',
      projectName: obj.projectName || '',
    }
  },
  updateProject (state, obj) {
    state.project = { ...state.project, ...obj }
  },
  setProjectList (state, list) {
    state.projectList = list.map((item) => ({
      projectId: item.projectId || '',
      projectName: item.projectName || '',
    }))
  },
  addProject (state, project) {
    state.projectList.unshift({
      projectId: project.projectId || '',
      projectName: project.projectName || '',
    })
  },
  deleteProject (state, projectId) {
    const idx = state.projectList.findIndex((item) => item.projectId === projectId)
    if (idx !== -1) {
      state.projectList.splice(idx, 1)
    }
  },
  setCompany (state, obj) {
    state.company = {
      companyId: obj.companyId || '',
      companyName: obj.companyName || '',
      ownerId: obj.ownerId || '',
      ownerName: obj.ownerName || '',
      userList: obj.userList || [],
    }
  },
  updateCompany (state, obj) {
    state.company = { ...state.company, ...obj }
  },
  setUser (state, obj) {
    state.user = {
      userId: obj.userId,
      userName: obj.userName,
      gradeId: obj.gradeId,
    }
  },
  updateUser (state, obj) {
    state.user = { ...state.user, ...obj }
  },
}
export default mutationObj
